package com.user.service.impl;

import java.util.Iterator;
import java.util.List;
import com.user.bean.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.user.mapper.*;
import com.user.service.*;

/*业务层：SixiouService接口的实现类*/
@Service
public class SixiouServiceImpl implements SixiouService {
//	从IOC容器中注入sixiouMapper对象
	@Autowired
	private SixiouMapper sixiouMapper;

//	查询所有数据
	public List<Sixiou> findAll() {
		// TODO Auto-generated method stub
		return findAll(null);
	}

//	根据whereWrap条件查询符合条件的数据
	public List<Sixiou> findAll(Sixiou whereWrap) {
		// TODO Auto-generated method stub
		return findAll(whereWrap, true);
	}

//	根据whereWrap条件和查询模式查询符合条件的数据
	public List<Sixiou> findAll(Sixiou whereWrap, boolean isDistance) {
		// TODO Auto-generated method stub
		List<Sixiou> list = sixiouMapper.selectByExample(whereWrap);
		if (whereWrap!=null) {
			Iterator<Sixiou> iterator = list.iterator();
			while (iterator.hasNext()) {
				Sixiou sixiou = (Sixiou) iterator.next();
				if (whereWrap.getId() != null&&(!(whereWrap.getId()+"").equals(""))) {
					if (isDistance) {
						if (!(sixiou.getId() + "").equals(whereWrap.getId() + "")) {
							iterator.remove();
							continue;
						}
					} else {
						if (!(sixiou.getId() + "").contains(whereWrap.getId() + "")) {
							iterator.remove();
							continue;
						}
					}
				}
				if (whereWrap.getTopic() != null&&(!(whereWrap.getTopic()+"").equals(""))) {
					if (isDistance) {
						if (!(sixiou.getTopic() + "").equals(whereWrap.getTopic() + "")) {
							iterator.remove();
							continue;
						}
					} else {
						if (!(sixiou.getTopic() + "").contains(whereWrap.getTopic() + "")) {
							iterator.remove();
							continue;
						}
					}
				}
				if (whereWrap.getTopic_ans() != null&&(!(whereWrap.getTopic_ans()+"").equals(""))) {
					if (isDistance) {
						if (!(sixiou.getTopic_ans() + "").equals(whereWrap.getTopic_ans() + "")) {
							iterator.remove();
							continue;
						}
					} else {
						if (!(sixiou.getTopic_ans() + "").contains(whereWrap.getTopic_ans() + "")) {
							iterator.remove();
							continue;
						}
					}
				}
				if (whereWrap.getAnswer() != null&&(!(whereWrap.getAnswer()+"").equals(""))) {
					if (isDistance) {
						if (!(sixiou.getAnswer() + "").equals(whereWrap.getAnswer() + "")) {
							iterator.remove();
							continue;
						}
					} else {
						if (!(sixiou.getAnswer() + "").contains(whereWrap.getAnswer() + "")) {
							iterator.remove();
							continue;
						}
					}
				}
				if (whereWrap.getGrad() != null&&(!(whereWrap.getGrad()+"").equals(""))) {
					if (isDistance) {
						if (!(sixiou.getGrad() + "").equals(whereWrap.getGrad() + "")) {
							iterator.remove();
							continue;
						}
					} else {
						if (!(sixiou.getGrad() + "").contains(whereWrap.getGrad() + "")) {
							iterator.remove();
							continue;
						}
					}
				}
				if (whereWrap.getMultiple() != null&&(!(whereWrap.getMultiple()+"").equals(""))) {
					if (isDistance) {
						if (!(sixiou.getMultiple() + "").equals(whereWrap.getMultiple() + "")) {
							iterator.remove();
							continue;
						}
					} else {
						if (!(sixiou.getMultiple() + "").contains(whereWrap.getMultiple() + "")) {
							iterator.remove();
							continue;
						}
					}
				}
			} 
		}
		return list;
	}

//	根据主键查询符合条件的数据
	public Sixiou findById(Integer id) {
		// TODO Auto-generated method stub
		return sixiouMapper.selectByPrimaryKey(id);
	}

//	插入一条sixiou数据
	public int insert(Sixiou sixiou) {
		// TODO Auto-generated method stub
		return sixiouMapper.insertSelective(sixiou);
	}

//	根据主键更新一条sixiou数据
	public int updateById(Sixiou sixiou) {
		// TODO Auto-generated method stub
		return sixiouMapper.updateByPrimaryKey(sixiou);
	}

//	根据主键选择性更新一条sixiou数据
	public int updateByIdSelective(Sixiou sixiou) {
		// TODO Auto-generated method stub
		Sixiou dbSixiou = findById(sixiou.getId());
		if (sixiou.getId()==null) {
			sixiou.setId(dbSixiou.getId());
		}
		if (sixiou.getTopic()==null) {
			sixiou.setTopic(dbSixiou.getTopic());
		}
		if (sixiou.getTopic_ans()==null) {
			sixiou.setTopic_ans(dbSixiou.getTopic_ans());
		}
		if (sixiou.getAnswer()==null) {
			sixiou.setAnswer(dbSixiou.getAnswer());
		}
		if (sixiou.getGrad()==null) {
			sixiou.setGrad(dbSixiou.getGrad());
		}
		if (sixiou.getMultiple()==null) {
			sixiou.setMultiple(dbSixiou.getMultiple());
		}
		return sixiouMapper.updateByPrimaryKey(sixiou);
	}

//	根据主键删除一条sixiou数据
	public int deleteById(Integer id) {
		// TODO Auto-generated method stub
		return sixiouMapper.deleteByPrimaryKey(id);
	}

//	根据whereWrap条件删除符合条件的数据
	public int delete(Sixiou whereWrap) {
		// TODO Auto-generated method stub
		return delete(whereWrap, true);
	}

//	根据whereWrap条件和查询模式删除符合条件的数据
	public int delete(Sixiou whereWrap, boolean isDistance) {
		// TODO Auto-generated method stub
		List<Sixiou> list = findAll(whereWrap, isDistance);
		for (Sixiou sixiou : list) {
			sixiouMapper.deleteByPrimaryKey(sixiou.getId());
		}
		return 0;
	}

//	根据主键集合批量删除sixiou数据
	public int deleteByIds(List<Integer> list) {
		// TODO Auto-generated method stub
		for (Integer id : list) {
			deleteById(id);
		}
		return 0;
	}
}

